package com.icinfo.cloud.provider.peripheralinterface.regulatory.service.impl;

import cn.hutool.http.HttpStatus;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.utils.HeaderUtil;
import com.icinfo.cloud.provider.peripheralinterface.regulatory.service.IRegulatoryService;
import com.icinfo.cloud.provider.peripheralinterface.taskcenter.dto.DisposalBackResultDto;
import com.icinfo.framework.common.web.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述：执法平台接口实现类
 *
 * @Author zhengqiang
 * @Date 2022/8/18 21:45
 **/
@Slf4j
@Service
public class RegulatoryServiceImpl implements IRegulatoryService {

    @Value("${regulatory.url}")
    private String regulatoryUrl;

    @Value("${regulatory.api.getListCataIdBy}")
    private String getListCataIdByApi;
    @Value("${regulatory.api.getAuditPubishTreeByOrgCode}")
    private String getAuditPubishTreeByOrgCodeApi;



    /**
     * 描述： 根据事项id获取父级id
     *
     * @author: zhengqiang
     * @date: 2022/8/16
     * @param: dto
     * @return {@link Result}
     **/
    @Override
    public Result getListCataIdByBusId(String busId) {
        log.info("调用执法平台查询父级事项id接口");
        Result result = null;
        try {
            Map<String, Object> urlParam = new HashMap<>();
            urlParam.put("busId", busId);
            log.info("getListCataIdBy -- 入参: {}", urlParam);
            String response = HttpUtil.createGet(regulatoryUrl.concat(getListCataIdByApi)).form(urlParam).execute().body();
            log.info("getListCataIdBy -- 出参: {}", response);
            result = JSONObject.parseObject(response, Result.class);
            if (HttpStatus.HTTP_OK == result.getCode() && !ObjectUtils.isEmpty(result.getData())){
                return Result.success(Constant.STRING_SUCCESS_EN, result.getData());
            }
        } catch (Exception e) {
            log.warn("getListCataIdBy: {}", e);
            e.printStackTrace();
        }
        return Result.error(Constant.STRING_FAIL_EN);
    }

    /**
     * 【V1.12.1】描述： 根据部门编码查询处罚事项
     *
     * @author: zhengqiang
     * @date: 2023/8/23
     * @param: orgCode
     * @return {@link Result}
     **/
    @Override
    public Result getAuditPubishTreeByOrgCode(String orgCode) {
        log.info("调用执法平台查询处罚事项接口");
        Result result = null;
        try {
            Map<String, String> headers = HeaderUtil.headerInfo();
            Map<String, Object> urlParam = new HashMap<>();
            urlParam.put("orgCode", orgCode);
            log.info("getAuditPubishTreeByOrgCode -- 入参: {}", urlParam);
            String response = HttpUtil.createGet(regulatoryUrl.concat(getAuditPubishTreeByOrgCodeApi)).addHeaders(headers).form(urlParam).execute().body();
            //log.info("getAuditPubishTreeByOrgCode -- 出参: {}", response);
            result = JSONObject.parseObject(response, Result.class);
            if (HttpStatus.HTTP_OK == result.getCode() && !ObjectUtils.isEmpty(result.getData())){
                return Result.success(Constant.STRING_SUCCESS_EN, result.getData());
            }
        } catch (Exception e) {
            log.warn("getAuditPubishTreeByOrgCode: {}", e);
            e.printStackTrace();
        }
        return Result.error(Constant.STRING_FAIL_EN);
    }

}
